package com.ciwong.epaper.modules.reciteWords.dao;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.HeadOverrideRequest;
import com.ciwong.epaper.modules.me.ui.JsonParserUtil;
import com.ciwong.epaper.modules.reciteWords.bean.LearnBookDetail;
import com.ciwong.epaper.modules.reciteWords.bean.LearnBookStatus;
import com.ciwong.epaper.modules.reciteWords.bean.LearnCountBean;
import com.ciwong.epaper.modules.reciteWords.bean.LearnedWordBean;
import com.ciwong.epaper.modules.reciteWords.bean.PlanAndWorListBean;
import com.ciwong.epaper.modules.reciteWords.bean.SaveAndChangePlanBean;
import com.ciwong.epaper.modules.reciteWords.bean.SelectVocabularyBean;
import com.ciwong.epaper.modules.reciteWords.bean.SubmitWordPostBean;
import com.ciwong.epaper.modules.reciteWords.bean.UnLearnWordBean;
import com.ciwong.epaper.modules.reciteWords.bean.WordPlanBean;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.BaseRequestUtil;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by keepon on 2018/9/13.
 */
public class WordRequest {

    public static void getRecitewordsVersion(final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, WordAction.ACTION_GET_RECITE_WORDS_VERSION);
        params.put("brandId", EApplication.BRAND_ID + "");
        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        callBack.failed(volleyError);
                    }
                }, new BaseRequest.ResponseCallback() {
            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });

        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<List<SelectVocabularyBean>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }


    public static void saveAndGetWordPlan(int userId, int bookId, String planName,
                                          int dayCount, int days, final BaseCallBack callBack) {
        final String postJson = JsonParserUtil.toJson(new SaveAndChangePlanBean(userId, bookId, planName, dayCount, days));
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                WordAction.ACTION_SAVE_USER_WORD_PALN);
        HeadOverrideRequest request = new HeadOverrideRequest(Request.Method.POST, params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    if (null != callBack)
                        callBack.success(obj);
                } else {
                    if (null != callBack)
                        callBack.failed(errorCode, msg);
                }
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return postJson.getBytes();
            }
        };
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(WordPlanBean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    public static void submitWord(int userId, ArrayList<Integer> wordArray, final BaseCallBack callBack) {
        if (wordArray == null || wordArray.size() == 0) {
            wordArray = new ArrayList<>();
        }
        final String postJson = JsonParserUtil.toJson(new SubmitWordPostBean(userId, wordArray));
        CWLog.d("byou", postJson);
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                WordAction.ACTION_SUBMIT_WORD);
        HeadOverrideRequest request = new HeadOverrideRequest(Request.Method.POST, params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode, int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    if (null != callBack)
                        callBack.success(obj);
                } else {
                    if (null != callBack)
                        callBack.failed(errorCode, msg);
                }
            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return postJson.getBytes();
            }
        };
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(PlanAndWorListBean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    public static void getLearnedWords(int userId, final BaseCallBack callBack) {

        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, WordAction.ACTION_LEARNED_WORD_LIST);
        params.put("userId", userId + "");

        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(LearnedWordBean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    public static void getUnlearnedWords(int userId, int pageIndex, int pagerSize, final BaseCallBack callBack) {

        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION, WordAction.ACTION_UNLEARN_WORD_LIST);
        params.put("userId", userId + "");
        params.put("pageindex", pageIndex + "");
        params.put("pagesize", pagerSize + "");

        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(UnLearnWordBean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());

    }

    /**
     * 当月已读单词统计
     *
     * @param userId
     * @param callBack
     */
    public static void getLearnCount(int userId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                WordAction.ACTION_GET_LEARN_COUNT);
        params.put("userId", userId + "");

        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(LearnCountBean.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    public static void getBookLearnInfo(int userId, int bookId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                WordAction.ACTION_GET_LEARIN_INFO);
        params.put("userId", userId + "");
        params.put("bookId", bookId + "");

        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        request.setResponseClazz(LearnBookDetail.class);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    public static void getBookLearning(int userId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                WordAction.ACTION_GET_BOOK_LEARNING);
        params.put("userId", userId + "");

        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseDataType(com.ciwong.libs.utils.volley.HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        Type responseType = new TypeToken<List<LearnBookStatus>>() {
        }.getType();
        request.setResponseType(responseType);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }

    /**
     * 再学习一遍
     *
     * @param userId
     * @param bookId
     * @param callBack
     */
    public static void getAgain(int userId, int bookId, final BaseCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(HttpRequest.REQUEST_MODULES, WordAction.class.getName());
        params.put(HttpRequest.REQUEST_ACTION,
                WordAction.ACTION_GET_AGAIN);
        params.put("userId", userId + "");
        params.put("bookId", bookId + "");

        HttpRequest request = new HttpRequest(params,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callBack.failed(error);
                    }
                }, new BaseRequest.ResponseCallback() {

            @Override
            public void error(int errorCode, String msg) {
                callBack.failed(errorCode, msg);
            }

            @Override
            public void success(Object obj, int resultCode,
                                int errorCode, String msg) {
                if (resultCode == 0 && errorCode == 0) {
                    callBack.success(obj);
                } else {
                    callBack.failed(errorCode, msg);
                }
            }
        });
        request.setResponseDataType(HttpRequest.RESPONSE_DATA_TYPE_OBJECT);
        BaseRequestUtil.getInstance().add(request);
        callBack.setUrl(request.getUrl());
    }
}
